﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Control.Bus;

namespace Control.Dal
{
    class DalExportBillDetail
    {
        internal static int Insert(Control.Bus.BusExportBillDetail busExportBillDetail)
        {
            ExportBillDetail BillDetail = new ExportBillDetail()
            {
                IDBill = busExportBillDetail.Bill.ID,
                IDProd = busExportBillDetail.Prod.ID,
                Quantity = busExportBillDetail.Quantity,
                RealQuantity=busExportBillDetail.RealQuantity,
                Recieve=busExportBillDetail.Recieve
                
            };
            DalCommon.Database.ExportBillDetails.InsertOnSubmit(BillDetail);
            DalCommon.Database.SubmitChanges();
            return BillDetail.ID;
        }

        internal static void Delete(Control.Bus.BusExportBillDetail busExportBillDetail)
        {
            WareHouseDataContext db = DalCommon.Database;
            try
            {
                ExportBillDetail BillDetail = db.ExportBillDetails.Single(b => b.ID == busExportBillDetail.ID);
                db.ExportBillDetails.DeleteOnSubmit(BillDetail);
                db.SubmitChanges();
            }
            catch { }
        }


        internal static void Update(Control.Bus.BusExportBillDetail busExportBillDetail)
        {
            WareHouseDataContext db = DalCommon.Database;
            ExportBillDetail bd = db.ExportBillDetails.Single(b => b.ID == busExportBillDetail.ID);
            bd.IDBill = busExportBillDetail.Bill.ID;
            bd.IDProd = busExportBillDetail.Prod.ID;
            bd.Quantity = busExportBillDetail.Quantity;
            bd.RealQuantity=busExportBillDetail.RealQuantity;
            bd.Recieve = busExportBillDetail.Recieve;
            db.SubmitChanges();
        }

        internal static Control.Bus.BusExportBillDetail GetByID(int p)
        {
            var bds = from bd in DalCommon.Database.ExportBillDetails
                      where bd.ID == p
                      select new BusExportBillDetail()
                      {
                          ID = bd.ID,
                          Bill = new BusExportBill()
                          {
                              ID = bd.ExportBill.ID,
                              Date = bd.ExportBill.Date,
                              Staff = new BusStaff()
                              {
                                  ID = bd.ExportBill.Staff.ID,
                                  UserName = bd.ExportBill.Staff.UserName,
                                  PassWord = bd.ExportBill.Staff.PassWord
                              }
                          },
                          Prod = BusProduct.GetProdByCode(bd.Product.ProdCode),
                          Quantity = (int)bd.Quantity,
                          RealQuantity = bd.RealQuantity,
                          Recieve = bd.Recieve
                      };
            return bds.FirstOrDefault<BusExportBillDetail>();
        }

        internal static List<BusExportBillDetail> GetListByBill(BusExportBill busExportBill)
        {
            var bds = from bd in DalCommon.Database.ExportBillDetails
                      where bd.IDBill==busExportBill.ID
                      select new BusExportBillDetail()
                      {
                          ID = bd.ID,
                          Bill = new BusExportBill()
                          {
                              ID = bd.ExportBill.ID,
                              Date = bd.ExportBill.Date,
                              Staff = new BusStaff()
                              {
                                  ID = bd.ExportBill.Staff.ID,
                                  UserName = bd.ExportBill.Staff.UserName,
                                  PassWord = bd.ExportBill.Staff.PassWord
                              }
                          },
                          Prod = BusProduct.GetProdByCode(bd.Product.ProdCode),
                          Quantity = (int)bd.Quantity,
                          RealQuantity = bd.RealQuantity,
                          Recieve = bd.Recieve,
                          IsSaved=true
                      };
            return bds.ToList<BusExportBillDetail>();
        }

        internal static List<BusExportBillDetail> Find(string MaBO, string TenBO, string MaProd, DateTime fromDate, DateTime toDate)
        {
            if (MaBO !="")
            {
                var bds = from bd in DalCommon.Database.ExportBillDetails
                          where bd.ExportBill.BillCode.Contains(MaBO) && bd.ExportBill.BillName.Contains(TenBO) && bd.Product.ProdCode.Contains(MaProd) && bd.ExportBill.Date.CompareTo(fromDate) >= 0 && bd.ExportBill.Date.CompareTo(toDate) <= 0
                          select new BusExportBillDetail()
                          {
                              ID = bd.ID,
                              Bill = new BusExportBill()
                              {
                                  ID = bd.ExportBill.ID,
                                  Date = bd.ExportBill.Date,
                                  Staff = new BusStaff()
                                  {
                                      ID = bd.ExportBill.Staff.ID,
                                      UserName = bd.ExportBill.Staff.UserName,
                                      PassWord = bd.ExportBill.Staff.PassWord
                                  }
                              },
                              Prod = BusProduct.GetProdByCode(bd.Product.ProdCode),
                              Quantity = (int)bd.Quantity,
                              RealQuantity = bd.RealQuantity,
                              Recieve = bd.Recieve,
                              IsSaved = true
                          };
                return bds.ToList<BusExportBillDetail>();
            }
            else
            {
                var bds = from bd in DalCommon.Database.ExportBillDetails
                          where bd.ExportBill.BillName.Contains(TenBO) && bd.Product.ProdCode.Contains(MaProd) && bd.ExportBill.Date.CompareTo(fromDate) >= 0 && bd.ExportBill.Date.CompareTo(toDate) <= 0
                          select new BusExportBillDetail()
                          {
                              ID = bd.ID,
                              Bill = new BusExportBill()
                              {
                                  ID = bd.ExportBill.ID,
                                  Date = bd.ExportBill.Date,
                                  Staff = new BusStaff()
                                  {
                                      ID = bd.ExportBill.Staff.ID,
                                      UserName = bd.ExportBill.Staff.UserName,
                                      PassWord = bd.ExportBill.Staff.PassWord
                                  }
                              },
                              Prod = BusProduct.GetProdByCode(bd.Product.ProdCode),
                              Quantity = (int)bd.Quantity,
                              RealQuantity = bd.RealQuantity,
                              Recieve = bd.Recieve,
                              IsSaved = true
                          };
                return bds.ToList<BusExportBillDetail>();
            }
        }
    }
}
